<template>
  <div class="acceptancelist-container">
    <div class="content">
      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getData"
        >
          <Card v-for="goods in goodslist" :key="goods.id" :data="goods" />
        </van-list>
      </div>
    </div>
  </div>
</template>

<script>
import { query } from "@/api/goodslist";
import {
  getCollectList,
  getLikeList,
  getOrderData,
} from "@/api/interchangeable";

import { transformTimestamp } from "@/tools/utils";
import Card from "@/components/Card.vue";
export default {
  name: "Goodslist",
  data() {
    return {
      loading: false,
      finished: false,
      curPage: 1,
      pageSize: 10,
      total: 0,
      goodslist: [],
      type: "",
    };
  },
  components: {
    Card,
  },
  watch: {},
  beforeCreate() {},
  created() {},
  mounted() {
    const { type } = this.$route.query;
    this.type = type;
    this.userData = JSON.parse(localStorage.getItem("userData")) || undefined;
    this.getData();
  },
  methods: {
    getData() {
      if (this.type == 1) {
        this.getcardData1();
      } else if (this.type == 2) {
        this.getcardData2();
      } else if (this.type == 3) {
        this.getcardData3();
      } else if (this.type == 4) {
        this.getcardData4();
      }
    },
    getcardData1() {
      this.loading = true;
      query({
        currPage: this.curPage,
        pageSize: this.pageSize,
        category_id: this.active,
        state: 2,
        user_id: this.userData.id,
      })
        .then((res) => {
          this.curPage = this.curPage + 1;
          this.goodslist = [
            ...this.goodslist,
            ...res.data.map((item) => {
              return {
                ...item,
                release_time: transformTimestamp(item.release_time),
              };
            }),
          ];
          this.total = res.count;
          if (this.goodslist.length >= res.count) {
            this.finished = true;
          }
          this.flot = false;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getcardData2() {
      this.loading = true;
      getOrderData({
        currPage: this.curPage,
        pageSize: this.pageSize,
        user_id: this.userData.id,
      })
        .then((res) => {
          this.curPage = this.curPage + 1;
          this.goodslist = [
            ...this.goodslist,
            ...res.data.map((item) => {
              return {
                userlist: item.userlist,
                ...item.goodlist[0],
                release_time: transformTimestamp(item.goodlist[0].release_time),
              };
            }),
          ];
          this.total = res.count;
          if (this.goodslist.length >= res.count) {
            this.finished = true;
          }
          this.flot = false;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getcardData3() {
      this.loading = true;
      getLikeList({
        currPage: this.curPage,
        pageSize: this.pageSize,
        user_id: this.userData.id,
      })
        .then((res) => {
          this.curPage = this.curPage + 1;
          this.goodslist = [
            ...this.goodslist,
            ...res.data.map((item) => {
              return {
                userlist: item.userlist,
                ...item.goodlist[0],
                release_time: transformTimestamp(item.goodlist[0].release_time),
              };
            }),
          ];
          this.total = res.count;
          if (this.goodslist.length >= res.count) {
            this.finished = true;
          }
          this.flot = false;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getcardData4() {
      this.loading = true;
      getCollectList({
        currPage: this.curPage,
        pageSize: this.pageSize,
        user_id: this.userData.id,
      })
        .then((res) => {
          this.curPage = this.curPage + 1;
          this.goodslist = [
            ...this.goodslist,
            ...res.data.map((item) => {
              return {
                userlist: item.userlist,
                ...item.goodlist[0],
                release_time: transformTimestamp(item.goodlist[0].release_time),
              };
            }),
          ];
          this.total = res.count;
          if (this.goodslist.length >= res.count) {
            this.finished = true;
          }
          this.flot = false;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
